package service

import (
	"user-service/pkg"

	"context"
	"strconv"
	"user-service/basic/config"
	__ "user-service/basic/proto"
	"user-service/models"
)

type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) UserInFoById(_ context.Context, in *__.UserInfoByIdRequest) (*__.UserInfoByIdResponse, error) {
	var user models.User
	user.Account = in.Account

	if err := user.GetUserByUsername(config.DB); err != nil {
		return nil, err
	}

	return &__.UserInfoByIdResponse{
		Account:  user.Account,
		Password: user.Password,
		UserId:   int64(user.ID),
		Salt:     user.Salt,
	}, nil
}
func (s *Server) Createduser(_ context.Context, in *__.CreateduserRequest) (*__.Createduseresponse, error) {
	var user models.User
	user.Account = in.Account
	user.Password = pkg.Md5(in.Password)
	user.Salt = strconv.Itoa(1)
	if err := user.Createuser(config.DB); err != nil {
		return nil, err
	}
	return &__.Createduseresponse{
		UserId: int64(user.ID),
	}, nil
}
